package c0907m.group2.aitsm.gui;

import c0907m.group2.aitsm.core.db.UserDb;
import c0907m.group2.aitsm.core.entity.UserInfo;
import c0907m.group2.aitsm.gui.component.DefaultPanel;
import c0907m.group2.aitsm.gui.component.StatusBar;
import c0907m.group2.aitsm.gui.handler.TableCellListener;
import c0907m.group2.aitsm.util.Helper;
import c0907m.group2.aitsm.util.Settings;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Group 2
 *         Created on Apr 24, 2011, 11:28:40 PM
 *         GUI form for Users' management
 */
public class User extends DefaultPanel {
    //default state on status bar when init
    public static final String DEFAULT_STATUS = "Add, update, delete users";
    private DefaultTableModel dtm;
    private StatusBar statusBar;
    private List<UserInfo> editingList;
    private UserDb userDB;
    private UserInfo selectedUser;
    public final Logger logger = Logger.getLogger(User.class.getName());

    public void setStatusBar(StatusBar sb) {
        this.statusBar = sb;
    }

    /**
     * Creates new form User
     */
    public User() {
        initComponents();
        pnToggle.setVisible(false);
        txtOldPw.setVisible(false);
        btnUpdate.setEnabled(false);
        statusBar = new StatusBar();
        userDB = new UserDb();
        editingList = new ArrayList<UserInfo>();
        dtm = (DefaultTableModel) tblUserList.getModel();
        loadUsersToTable();
        tblUserListCellHandler();
    }

    /**
     * Reload all data from database
     */
    @Override
    public void refresh() {
        dtm.setRowCount(0);
        doReset();
        loadUsersToTable();
    }

    /**
     * Update changes
     */
    @Override
    public void doUpdate() {
        if (editingList.size() == 0) {
            statusBar.setMessage("No changes have been made: Update cancelled.", StatusBar.WARNING_TYPE);
            return;
        }

        try {
            userDB.update(editingList);
        } catch (SQLException e) {
            logger.log(Level.WARNING, "Update failed: " + e.getMessage());
            statusBar.setMessage("Update failed.", StatusBar.ERROR_TYPE);
            refresh();
        }

        statusBar.setMessage("Update successful.", StatusBar.MESSAGE_TYPE);
        editingList.removeAll(editingList);
    }

    /**
     * Add new User to database
     */
    @Override
    public void doAdd() {
        String pattern = "^\\w+([-.]\\w+)*@\\w+([-.]\\w+)*.\\w+$";

        if (txtName.getText().trim().equals("") |
                txtEmail.getText().trim().equals("") |
                txtMobile.getText().trim().equals("")) {

            JOptionPane.showMessageDialog(
                    this,
                    "Please fill all required fields!",
                    "Message",
                    JOptionPane.ERROR_MESSAGE
            );

        } else if (!txtEmail.getText().trim().toLowerCase().matches(pattern)) {
            JOptionPane.showMessageDialog(
                    this,
                    "Invalid email address!",
                    "Message",
                    JOptionPane.ERROR_MESSAGE
            );
        } else {
            UserInfo newUser = new UserInfo();
            newUser.setName(txtName.getText().trim());
            newUser.setPassword(Helper.MD5("123456"));
            newUser.setEmail(txtEmail.getText().trim());
            newUser.setMobile(txtMobile.getText().trim());
            newUser.setDescription(txaDescription.getText().trim());
            newUser.setAdmin(rdoAdmin.isSelected());

            try {
                userDB.insert(newUser);
                statusBar.setMessage("User added", StatusBar.MESSAGE_TYPE);
//                dtm.addRow(new Object[] {newUser.getId(),
//                                    newUser.getName(),
//                                    newUser.getDescription(),
//                                    newUser.isActive()});
                refresh();
            } catch (SQLException ex) {
                logger.log(Level.SEVERE, ex.getMessage());
                statusBar.setMessage("Adding failed.", StatusBar.ERROR_TYPE);
            }

        }
    }

    /**
     * Reset all fields to blank
     */
    @Override
    public void doReset() {
        txtName.setText("");
        txtEmail.setText("");
        txtMobile.setText("");
        txaDescription.setText("");
        txtSearchByName.setText("");
        txtSearchById.setText("");
        rdoAdmin.setSelected(false);
    }

    /**
     * Search by Id
     */
    public void idSearch() {
        int getId;
        UserInfo result;

        try {
            getId = Integer.parseInt(txtSearchById.getText());
        } catch (NumberFormatException e) {
            new JOptionPane().showMessageDialog(
                    this,
                    "Wrong input format!",
                    "Message",
                    JOptionPane.ERROR_MESSAGE
            );
            return;
        }

        try {
            if (dtm.getRowCount() != 0) dtm.setRowCount(0);
            result = userDB.select(getId);
            if (result != null) {
                statusBar.setMessage("1 match(es) found.", StatusBar.MESSAGE_TYPE);

                dtm.addRow(new Object[]{
                        result.getId(),
                        result.getName(),
                        result.getEmail(),
                        result.getMobile(),
                        result.getDescription(),
                        result.isAdmin(),
                        result.isActive()
                });
            } else {
                statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getMessage());
            statusBar.setMessage("Search stopped.", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * Search by name
     */
    public void nameSearch() {
        String name = txtSearchByName.getText();
        List<UserInfo> result;

        try {
            result = userDB.select(name);
            if (result.size() != 0) {
                statusBar.setMessage(result.size() + " match(es) found.", StatusBar.MESSAGE_TYPE);

                dtm.setRowCount(0);

                for (UserInfo u : result)
                    dtm.addRow(new Object[]{
                            u.getId(),
                            u.getName(),
                            u.getEmail(),
                            u.getMobile(),
                            u.getDescription(),
                            u.isAdmin(),
                            u.isActive()
                    });
            } else {
                statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getMessage());
            statusBar.setMessage("Search stopped.", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * Load all Users from database to table when initialize
     */
    private void loadUsersToTable() {
        userDB = new UserDb();
        List<UserInfo> UserList = null;
        try {
            UserList = userDB.select();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
            statusBar.setMessage("Unable to load data.", StatusBar.WARNING_TYPE);
        }

        dtm = (DefaultTableModel) tblUserList.getModel();
        for (UserInfo r : UserList) {
            dtm.addRow(new Object[]{
                    r.getId(),
                    r.getName(),
                    r.getEmail(),
                    r.getMobile(),
                    r.getDescription(),
                    r.isAdmin(),
                    r.isActive()
            });
        }
    }

    public void loadUserPassword() {
        int row = tblUserList.getSelectedRow();
        int getId = (Integer) dtm.getValueAt(row, 0);
        try {
            selectedUser = userDB.select(getId);
            txtOldPw.setText(selectedUser.getPassword());
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, ex.getSQLState());
        }

    }

    public void changePassword() {
        if (!Arrays.equals(jpfNewPw.getPassword(), jpfConfirmPw.getPassword())) {
            JOptionPane.showMessageDialog(
                    this,
                    "Password do not match",
                    "Message",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }

        int confirmChanges = JOptionPane.showConfirmDialog(
                this,
                "Are you sure want to change " + selectedUser.getName() + "'s password?",
                "Confirm",
                JOptionPane.OK_CANCEL_OPTION);

        if (confirmChanges == JOptionPane.OK_OPTION) {
            selectedUser.setPassword(Helper.MD5(String.copyValueOf(jpfNewPw.getPassword())));

            try {
                userDB.update(selectedUser);
            } catch (SQLException e) {
                logger.log(Level.WARNING, e.getSQLState());
                statusBar.setMessage("Unable to change password.", StatusBar.ERROR_TYPE);
            }

            jpfNewPw.setText("");
            jpfConfirmPw.setText("");
            statusBar.setMessage("Applied new password for user " + ""
                    + selectedUser.getName(), StatusBar.MESSAGE_TYPE);
        }

    }

    public void tblUserListCellHandler() {
        Action action = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                btnUpdate.setEnabled(true);
                TableCellListener tcl = (TableCellListener) e.getSource();

                int row = tcl.getRow();
                int column = tcl.getColumn();

                int getId = (Integer) dtm.getValueAt(row, 0);
                try {
                    selectedUser = userDB.select(getId);
                    txtOldPw.setText(selectedUser.getPassword());
                } catch (SQLException ex) {
                    logger.log(Level.SEVERE, ex.getSQLState());
                }

                UserInfo userToUpdate = new UserInfo();
                try {
                    userToUpdate.setId((Integer) dtm.getValueAt(row, 0));
                    userToUpdate.setName((String) dtm.getValueAt(row, 1));
                    userToUpdate.setEmail((String) dtm.getValueAt(row, 2));
                    userToUpdate.setMobile((String) dtm.getValueAt(row, 3));
                    userToUpdate.setDescription((String) dtm.getValueAt(row, 4));
                    userToUpdate.setAdmin((Boolean) dtm.getValueAt(row, 5));
                    userToUpdate.setActive((Boolean) dtm.getValueAt(row, 6));
                    userToUpdate.setPassword(txtOldPw.getText());

                    editingList.add(userToUpdate);
                } catch (NumberFormatException e1) {
                    JOptionPane.showMessageDialog(
                            null,
                            "Wrong format!",
                            "Warning",
                            JOptionPane.ERROR_MESSAGE
                    );
                    dtm.setValueAt(tcl.getOldValue(), row, column);
                } catch (Exception e2) {
                    logger.log(Level.SEVERE, e2.getMessage());
                    statusBar.setMessage("Unknown", StatusBar.ERROR_TYPE);
                }

                //System.out.println("Event fired!");
            }
        };

        TableCellListener tcl = new TableCellListener(tblUserList, action);
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        pnRoleList = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblUserList = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        btnRefresh = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtSearchByName = new javax.swing.JTextField();
        btnSearchByName = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        btnDropDown = new javax.swing.JButton();
        pnToggle = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtSearchById = new javax.swing.JTextField();
        btnSearchById = new javax.swing.JButton();
        pnUserInfo = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        txtName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        rdoAdmin = new javax.swing.JRadioButton();
        jLabel8 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtMobile = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        lblChangePassword = new javax.swing.JLabel();
        txtOldPw = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        jpfNewPw = new javax.swing.JPasswordField();
        jpfConfirmPw = new javax.swing.JPasswordField();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(850, 430));

        btnAdd.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/add-16.png"));
        btnAdd.setText("Add");
        btnAdd.setMaximumSize(new java.awt.Dimension(61, 23));
        btnAdd.setMinimumSize(new java.awt.Dimension(61, 23));
        btnAdd.setPreferredSize(new java.awt.Dimension(61, 23));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/update-16.png"));
        btnUpdate.setText("Update");
        btnUpdate.setMaximumSize(new java.awt.Dimension(87, 23));
        btnUpdate.setMinimumSize(new java.awt.Dimension(87, 23));
        btnUpdate.setPreferredSize(new java.awt.Dimension(87, 23));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        pnRoleList.setBackground(new java.awt.Color(255, 255, 255));
        pnRoleList.setBorder(javax.swing.BorderFactory.createTitledBorder("User list"));

        tblUserList.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{

                },
                new String[]{
                        "ID", "Name", "Email", "Mobile", "Description", "Admin", "Active"
                }
        ) {
            Class[] types = new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean[]{
                    false, true, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        tblUserList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblUserListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblUserList);
        tblUserList.getColumnModel().getColumn(0).setMinWidth(20);
        tblUserList.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblUserList.getColumnModel().getColumn(0).setMaxWidth(30);
        tblUserList.getColumnModel().getColumn(1).setPreferredWidth(60);
        tblUserList.getColumnModel().getColumn(2).setPreferredWidth(60);
        tblUserList.getColumnModel().getColumn(3).setPreferredWidth(60);
        tblUserList.getColumnModel().getColumn(4).setPreferredWidth(80);
        tblUserList.getColumnModel().getColumn(5).setPreferredWidth(50);
        tblUserList.getColumnModel().getColumn(6).setMinWidth(20);
        tblUserList.getColumnModel().getColumn(6).setPreferredWidth(50);

        javax.swing.GroupLayout pnRoleListLayout = new javax.swing.GroupLayout(pnRoleList);
        pnRoleList.setLayout(pnRoleListLayout);
        pnRoleListLayout.setHorizontalGroup(
                pnRoleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnRoleListLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 433, Short.MAX_VALUE)
                                .addContainerGap())
        );
        pnRoleListLayout.setVerticalGroup(
                pnRoleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnRoleListLayout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                .addContainerGap())
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setForeground(new java.awt.Color(0, 102, 255));
        jLabel4.setText("MANAGE USERS");

        btnRefresh.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/refresh-16.gif"));
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Search for user"));
        jPanel1.setMaximumSize(new java.awt.Dimension(452, 219));
        jPanel1.setMinimumSize(new java.awt.Dimension(452, 219));

        jLabel5.setText("Search by name:");

        btnSearchByName.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/search-16.png"));
        btnSearchByName.setText("Search");
        btnSearchByName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchByNameActionPerformed(evt);
            }
        });

        jLabel6.setText("Advance search");

        btnDropDown.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/dropDown_icon.png"));
        btnDropDown.setBorderPainted(false);
        btnDropDown.setContentAreaFilled(false);
        btnDropDown.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnDropDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDropDownActionPerformed(evt);
            }
        });

        pnToggle.setBackground(new java.awt.Color(255, 255, 255));

        jLabel7.setText("Search by ID:");

        btnSearchById.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/search-16.png"));
        btnSearchById.setText("Search");
        btnSearchById.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchByIdActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnToggleLayout = new javax.swing.GroupLayout(pnToggle);
        pnToggle.setLayout(pnToggleLayout);
        pnToggleLayout.setHorizontalGroup(
                pnToggleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnToggleLayout.createSequentialGroup()
                                .addGroup(pnToggleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel7)
                                        .addGroup(pnToggleLayout.createSequentialGroup()
                                                .addComponent(txtSearchById, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnSearchById)))
                                .addContainerGap(31, Short.MAX_VALUE))
        );
        pnToggleLayout.setVerticalGroup(
                pnToggleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnToggleLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(pnToggleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtSearchById, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnSearchById, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(pnToggle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(txtSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnSearchByName))
                                        .addComponent(jLabel5)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(btnDropDown, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(4, 4, 4)
                                                .addComponent(jLabel6)))
                                .addContainerGap(67, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnSearchByName, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel6))
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addGap(8, 8, 8)
                                                .addComponent(btnDropDown, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pnToggle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(27, 27, 27))
        );

        pnUserInfo.setBackground(new java.awt.Color(255, 255, 255));
        pnUserInfo.setBorder(javax.swing.BorderFactory.createTitledBorder("Add new user"));
        pnUserInfo.setPreferredSize(new java.awt.Dimension(452, 219));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Name:");
        jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel3.setText("Description:");

        txaDescription.setColumns(20);
        txaDescription.setRows(5);
        jScrollPane1.setViewportView(txaDescription);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Set admin:");

        rdoAdmin.setBackground(new java.awt.Color(255, 255, 255));

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Email:");

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Mobile:");

        javax.swing.GroupLayout pnUserInfoLayout = new javax.swing.GroupLayout(pnUserInfo);
        pnUserInfo.setLayout(pnUserInfoLayout);
        pnUserInfoLayout.setHorizontalGroup(
                pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnUserInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                                        .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                                        .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(pnUserInfoLayout.createSequentialGroup()
                                                        .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                                        .addGap(184, 184, 184))
                                                .addGroup(pnUserInfoLayout.createSequentialGroup()
                                                        .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                .addComponent(txtMobile, javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(txtEmail, javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                                                        .addContainerGap()))
                                        .addComponent(rdoAdmin)))
        );
        pnUserInfoLayout.setVerticalGroup(
                pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnUserInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3)
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel9)
                                        .addComponent(txtMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(11, 11, 11)
                                .addGroup(pnUserInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(rdoAdmin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Change user's password"));

        txtOldPw.setEditable(false);

        jLabel11.setText("New password:");

        jLabel12.setText("Confirm password:");

        btnSave.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/save_btn.gif"));
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(jLabel11)
                                        .addComponent(jLabel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(txtOldPw))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jpfConfirmPw, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                        .addComponent(jpfNewPw, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                                        .addComponent(lblChangePassword))
                                .addContainerGap(85, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(txtOldPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(lblChangePassword, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel11)
                                        .addComponent(jpfNewPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel12)
                                        .addComponent(jpfConfirmPw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addContainerGap()
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel4)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                        .addComponent(pnUserInfo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                                                                        .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(pnRoleList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(293, 293, 293)
                                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(jLabel4)
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                .addComponent(pnRoleList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(pnUserInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(107, 107, 107))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        doAdd();
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnUpdateActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        doUpdate();
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void tblUserListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblUserListMouseClicked
        btnUpdate.setEnabled(true);
        loadUserPassword();
        lblChangePassword.setText("Set new password for user "
                + selectedUser.getName()
                + ":");
    }//GEN-LAST:event_tblUserListMouseClicked

    private void btnRefreshActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        refresh();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnSearchByNameActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSearchByNameActionPerformed
        nameSearch();
    }//GEN-LAST:event_btnSearchByNameActionPerformed

    private void btnDropDownActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnDropDownActionPerformed
        Helper.toggle(pnToggle);
    }//GEN-LAST:event_btnDropDownActionPerformed

    private void btnSearchByIdActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSearchByIdActionPerformed
        idSearch();
    }//GEN-LAST:event_btnSearchByIdActionPerformed

    private void btnSaveActionPerformed(ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        changePassword();
    }//GEN-LAST:event_btnSaveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDropDown;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearchById;
    private javax.swing.JButton btnSearchByName;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPasswordField jpfConfirmPw;
    private javax.swing.JPasswordField jpfNewPw;
    private javax.swing.JLabel lblChangePassword;
    private javax.swing.JPanel pnRoleList;
    private javax.swing.JPanel pnToggle;
    private javax.swing.JPanel pnUserInfo;
    private javax.swing.JRadioButton rdoAdmin;
    private javax.swing.JTable tblUserList;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtMobile;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtOldPw;
    private javax.swing.JTextField txtSearchById;
    private javax.swing.JTextField txtSearchByName;
    // End of variables declaration//GEN-END:variables

}
